<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>保证文件名唯一</title>
</head>

<body>
  <script>
    var getFolderNames = function (names) {
      let d = {}, ans = []
      for (const name of names) {
        let s = name
        while (s in d) {
          s = name + '(' + d[name] + ')'
          ++d[name]
        }
        d[s] = 1
        ans.push(s)
      }
      return ans
    };
    console.log(getFolderNames(["gta", "gta(1)", "gta", "avalon"]));
  </script>
</body>

</html>